
package com.gxu.sjjg;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.*;
import javax.swing.*;
import javax.swing.border.*;
public class AddManager extends JFrame {
    public AddManager() {
        initComponents();
    }

    //添加管理员
    private void ok(ActionEvent e) {
        File fmanager =new File("src/file/manager.txt");

        try {
            String praname= textField1.getText(), prapasswd=new String(passwordField1.getPassword());//获取输入框数据
            FileWriter infw = new FileWriter(fmanager,true);
            BufferedWriter inbf=new BufferedWriter(infw);
            File f = new File("src/file/manager.txt");
            try {
                FileReader fr = new FileReader(f);
                BufferedReader bfr = new BufferedReader(fr);
                String s;
                boolean exsist = false;//标记是否添加成功
                if(praname.trim().equals("")||prapasswd.trim().equals(""))//不能只有空格字符或为空
                    JOptionPane.showMessageDialog(null, "用户名或密码不能为空！", "提示！", JOptionPane.WARNING_MESSAGE);
                else{
                    praname="用户名："+praname;//注意判断空后再加"用户名："！！
                    prapasswd="密码："+prapasswd;
                    while ((s = bfr.readLine()) != null) {////查询输入的数据中的用户名是否已经存在，即是否该管理员的信息已经添加！
                        if (s.equals(praname)) {
                            if (bfr.readLine().equals(prapasswd)) {
                                exsist = true;
                                break;
                            }
                        }
                    }
                    if (exsist)
                        JOptionPane.showMessageDialog(null, "已存在，请勿重复添加！", "提示", JOptionPane.WARNING_MESSAGE);
                    else{
                        if(praname.trim().equals("")||prapasswd.trim().equals(""))
                            JOptionPane.showMessageDialog(null, "用户名或密码不能为空！", "提示！", JOptionPane.WARNING_MESSAGE);
                        else {
                            inbf.write(praname);
                            inbf.newLine();//换行
                            inbf.write(prapasswd);
                            inbf.newLine();
                            inbf.close();
                            infw.close();
                            JOptionPane.showMessageDialog(null, "添加成功！", "提示！", JOptionPane.INFORMATION_MESSAGE);
                        }
                    }
                }
                fr.close();
                bfr.close();
            } catch (FileNotFoundException ex) {
                ex.printStackTrace();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        catch (Exception ee){
            ee.printStackTrace();
        }
    }
    private void button1(ActionEvent e) {//退出按钮功能
        dispose();
    }
    //界面代码
    private void initComponents() {
        panel1 = new JPanel();
        manager = new JLabel();
        textField1 = new JTextField();
        password = new JLabel();
        passwordField1 = new JPasswordField();
        ok = new JButton();
        button1 = new JButton();
        label3 = new JLabel();

        //======== this ========
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);
        setTitle("\u6dfb\u52a0\u7ba1\u7406\u5458");
        setVisible(true);
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //======== panel1 ========
        {
            panel1.setLayout(null);

            //---- manager ----
            manager.setText("\u7528\u6237\u540d:");
            manager.setForeground(Color.black);
            panel1.add(manager);
            manager.setBounds(115, 55, 49, manager.getPreferredSize().height);

            //---- textField1 ----
            textField1.setOpaque(false);
            textField1.setBorder(new SoftBevelBorder(SoftBevelBorder.LOWERED));
            panel1.add(textField1);
            textField1.setBounds(165, 50, 135, 25);

            //---- password ----
            password.setText("\u5bc6   \u7801:");
            password.setForeground(Color.black);
            panel1.add(password);
            password.setBounds(115, 110, 45, password.getPreferredSize().height);

            //---- passwordField1 ----
            passwordField1.setOpaque(false);
            passwordField1.setBorder(new SoftBevelBorder(SoftBevelBorder.LOWERED));
            passwordField1.setSelectionEnd(300);
            panel1.add(passwordField1);
            passwordField1.setBounds(165, 105, 135, 25);

            //---- ok ----
            ok.setText("\u6dfb\u52a0");
            ok.setBorder(new SoftBevelBorder(SoftBevelBorder.LOWERED));
            ok.setContentAreaFilled(false);
            ok.addActionListener(e -> {
			ok(e);
		});
            panel1.add(ok);
            ok.setBounds(100, 170, 70, 25);

            //---- button1 ----
            button1.setText("\u8fd4\u56de");
            button1.setOpaque(false);
            button1.setContentAreaFilled(false);
            button1.setBorder(new SoftBevelBorder(SoftBevelBorder.LOWERED));
            button1.addActionListener(e -> button1(e));
            panel1.add(button1);
            button1.setBounds(215, 170, 70, 25);

            //---- label3 ----
            label3.setText("text");
            label3.setIcon(new ImageIcon(getClass().getResource("/myimage/loginimg.jpg")));
            panel1.add(label3);
            label3.setBounds(0, 0, 395, 270);

            {
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < panel1.getComponentCount(); i++) {
                    Rectangle bounds = panel1.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel1.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel1.setMinimumSize(preferredSize);
                panel1.setPreferredSize(preferredSize);
            }
        }
        contentPane.add(panel1);
        panel1.setBounds(0, 0, 395, 270);

        contentPane.setPreferredSize(new Dimension(395, 300));
        pack();
        setLocationRelativeTo(getOwner());
    }

    private JPanel panel1;
    private JLabel manager;
    private JTextField textField1;
    private JLabel password;
    private JPasswordField passwordField1;
    private JButton ok;
    private JButton button1;
    private JLabel label3;
}
